package mis.module.base.action;

import java.util.List;

import mis.core.action.BaseAction;
import mis.core.action.Json;
import mis.core.util.ExceptionUtil;
import mis.module.base.model.Iproduct;
import mis.module.base.model.Product;
import mis.module.base.model.ProductType;
import mis.module.base.service.ProductService;
import mis.module.base.service.ProductTypeService;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
 * 客户控制器 ProductAction
 *
 */
@Controller
@Action(value = "product", results = { @Result(name = "product", location = "/page/base/product.jsp") })
public class ProductAction extends BaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2071693874991450719L;
	private String productNo;
	private String productName;//productName
	private String description;
	private double price;
	private String productType;//productType
	
	private static final Logger logger = Logger.getLogger(ProductAction.class);
	@Autowired
	private ProductService productService;
	@Autowired
	private ProductTypeService productTypeService;
	
	public Product product = new Product();
	/**
	 * 得到数据列表
	 */
	public void grid(){
		int total = productService.getTotal();
		List<Product> list = productService.findAllProduct(page, rows, sort, order);	
		List<Iproduct> tempList = productService.changeList(list);
		writeJson(total,tempList);
	}
	/**
	 * 添加操作
	 */
	public void add(){
			Json result = new Json();
			ProductType iproductType;
			try {
				iproductType = productTypeService.getProductTypeById(Integer.parseInt(productType.trim()));
				if(iproductType.equals(null))
					System.out.println("iproductType is null");
				product.setId(this.getId());
				product.setDescription(this.getDescription()); 
				product.setPrice(this.getPrice());
				product.setProductName(this.getProductName());
				product.setProductNo(this.getProductNo());
				product.setProductType(iproductType);
				
				productService.saveProduct(product);
				result.setSuccess(true);
				result.setMsg("添加产品成功");					
			} catch (Exception e) {
				System.out.println("添加产品失败");
				e.printStackTrace();
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加产品失败");					
			}
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	public void edit() {
			Json result = new Json();
			ProductType iproductType;
			try {
				iproductType = productTypeService.getProductTypeById(Integer.parseInt(productType.trim()));
				product.setProductType(iproductType);
				product.setDescription(this.getDescription()); 
				product.setPrice(this.getPrice());
				product.setProductName(this.getProductName());
				product.setProductNo(this.getProductNo());
				product.setId(this.getId());
				
				productService.updateProduct(product);
				result.setSuccess(true);
				result.setMsg("修改产品成功");
				result.setSuccess(true);
			} catch (Exception e) {
				System.out.println("ProductAction edit failure-------");
				e.printStackTrace();
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加产品失败");
			}
			writeJson(result);
	}
	/**
	 * 删除操作
	 */
	public void remove() {
			Json result = new Json();
			try {
				productService.deleteProduct(this.getId());
				result.setSuccess(true);
				result.setMsg("删除产品成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除产品失败");
			}
			writeJson(result);
	}
	public String getProductNo() {
		return productNo;
	}
	public void setProductNo(String productNo) {
		this.productNo = productNo;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getProductType() {
		return productType;
	}
	public void setProductType(String productType) {
		this.productType = productType;
	}
	
}
